package com.base.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.base.service.DepartmentService;
import com.base.service.DictService;
import com.base.service.FlowtemplatedetailService;
import com.base.service.FlowworkitemService;
import com.base.service.RuleService;
import com.common.bean.Datadict;
import com.common.bean.Department;
import com.common.bean.Flowtemplatedetail;
import com.common.bean.Flowworkitem;
import com.common.bean.Rule;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;

public class FlowtemplatedetailManagerAction extends AbstractActionSupport
		implements Preparable {
	private static final long serialVersionUID = -46451872225336L;

	private FlowtemplatedetailService flowtemplatedetailService;

	private DepartmentService departmentService;

	private DictService dictService;

	private FlowworkitemService flowworkitemService;

	private RuleService ruleService;

	private Flowtemplatedetail flowtemplatedetail;

	private long workflowid;

	private PagerModel pm;

	private List<Flowworkitem> flowworkitemlist;

	private List<Department> departmentlist;

	private List<Datadict> datadictlist;// 流程步骤状态

	private List<Datadict> datadictByBranchlist;

	private List<Rule> rulelist;

	@SuppressWarnings("unchecked")
	public void prepare() throws Exception {
		flowworkitemlist = flowworkitemService.findAllFlowworkitem();
		departmentlist = departmentService.findAllleafDepartment();
		datadictlist = dictService
				.findDictDetail(StringToken.DICT_FLOWDETAILSTATUS);
		datadictByBranchlist = dictService
				.findDictDetail(StringToken.DICT_FLOWBRANCH);
		rulelist = ruleService.findAllRule();
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String goAddFlowtemplatedetail() throws Exception {
		return SUCCESS;
	}

	public String addFlowtemplatedetail() throws Exception {
		flowtemplatedetailService.addFlowtemplatedetail(flowtemplatedetail);
		return SUCCESS;
	}

	public String findAllFlowtemplatedetail() throws Exception {
		pm = flowtemplatedetailService.findFlowtemplatedetailForPager();
		return SUCCESS;
	}

	public String findFlowtemplatedetail() throws Exception {
		flowtemplatedetail = flowtemplatedetailService
				.findFlowtemplatedetail(workflowid);
		return SUCCESS;
	}

	public String delFlowtemplatedetail() throws Exception {
		flowtemplatedetailService.removeFlowtemplatedetail(workflowid);
		return SUCCESS;
	}

	public String getFlowtemplatedetailForUpdate() throws Exception {
		flowtemplatedetail = flowtemplatedetailService
				.findFlowtemplatedetail(workflowid);
		return SUCCESS;
	}

	public String updateFlowtemplatedetail() throws Exception {
		flowtemplatedetailService.updateFlowtemplatedetail(flowtemplatedetail);
		return SUCCESS;
	}

	/**
	 * @author Neo_
	 * @return
	 * @throws Exception
	 */
	public void addFlowdetail()  {
		// flowtemplatedetailService.updateFlowtemplatedetail(flowtemplatedetail);

		HttpServletRequest request = ServletActionContext.getRequest();
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader(
					(ServletInputStream) request.getInputStream()));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			System.out.println(sb.toString());
		      
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	/**	
		HttpServletResponse response = ServletActionContext.getResponse();

		//response.setContentType("text/xml;charset=utf-8");
		
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");

		PrintWriter pw = null;
		try {
			pw = response.getWriter(); // 输出中文，这一句一定要放到response.setContentType("text/xml;charset=utf-8"),
			// response.setHeader("Cache-Control",
			// "no-cache")后面，否则中文返回到页面是乱码
 			pw.print("成功!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
	   pw.close();
		} 
		//return "success";// 这里返回的是null
*/
	}

	/**
	 * @return the datadictlist
	 */
	public List<Datadict> getDatadictlist() {
		return datadictlist;
	}

	/**
	 * @param datadictlist
	 *            the datadictlist to set
	 */
	public void setDatadictlist(List<Datadict> datadictlist) {
		this.datadictlist = datadictlist;
	}

	/**
	 * @return the departmentlist
	 */
	public List<Department> getDepartmentlist() {
		return departmentlist;
	}

	/**
	 * @param departmentlist
	 *            the departmentlist to set
	 */
	public void setDepartmentlist(List<Department> departmentlist) {
		this.departmentlist = departmentlist;
	}

	/**
	 * @return the departmentService
	 */
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	/**
	 * @param departmentService
	 *            the departmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/**
	 * @return the dictService
	 */
	public DictService getDictService() {
		return dictService;
	}

	/**
	 * @param dictService
	 *            the dictService to set
	 */
	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}

	/**
	 * @return the flowtemplatedetail
	 */
	public Flowtemplatedetail getFlowtemplatedetail() {
		return flowtemplatedetail;
	}

	/**
	 * @param flowtemplatedetail
	 *            the flowtemplatedetail to set
	 */
	public void setFlowtemplatedetail(Flowtemplatedetail flowtemplatedetail) {
		this.flowtemplatedetail = flowtemplatedetail;
	}

	/**
	 * @return the flowtemplatedetailService
	 */
	public FlowtemplatedetailService getFlowtemplatedetailService() {
		return flowtemplatedetailService;
	}

	/**
	 * @param flowtemplatedetailService
	 *            the flowtemplatedetailService to set
	 */
	public void setFlowtemplatedetailService(
			FlowtemplatedetailService flowtemplatedetailService) {
		this.flowtemplatedetailService = flowtemplatedetailService;
	}

	/**
	 * @return the flowworkitemlist
	 */
	public List<Flowworkitem> getFlowworkitemlist() {
		return flowworkitemlist;
	}

	/**
	 * @param flowworkitemlist
	 *            the flowworkitemlist to set
	 */
	public void setFlowworkitemlist(List<Flowworkitem> flowworkitemlist) {
		this.flowworkitemlist = flowworkitemlist;
	}

	/**
	 * @return the flowworkitemService
	 */
	public FlowworkitemService getFlowworkitemService() {
		return flowworkitemService;
	}

	/**
	 * @param flowworkitemService
	 *            the flowworkitemService to set
	 */
	public void setFlowworkitemService(FlowworkitemService flowworkitemService) {
		this.flowworkitemService = flowworkitemService;
	}

	/**
	 * @return the pm
	 */
	public PagerModel getPm() {
		return pm;
	}

	/**
	 * @param pm
	 *            the pm to set
	 */
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the rulelist
	 */
	public List<Rule> getRulelist() {
		return rulelist;
	}

	/**
	 * @param rulelist
	 *            the rulelist to set
	 */
	public void setRulelist(List<Rule> rulelist) {
		this.rulelist = rulelist;
	}

	/**
	 * @return the ruleService
	 */
	public RuleService getRuleService() {
		return ruleService;
	}

	/**
	 * @param ruleService
	 *            the ruleService to set
	 */
	public void setRuleService(RuleService ruleService) {
		this.ruleService = ruleService;
	}

	/**
	 * @return the workflowid
	 */
	public long getWorkflowid() {
		return workflowid;
	}

	/**
	 * @param workflowid
	 *            the workflowid to set
	 */
	public void setWorkflowid(long workflowid) {
		this.workflowid = workflowid;
	}

	public List<Datadict> getDatadictByBranchlist() {
		return datadictByBranchlist;
	}

	public void setDatadictByBranchlist(List<Datadict> datadictByBranchlist) {
		this.datadictByBranchlist = datadictByBranchlist;
	}

}
